import {Plugin} from '../extend';
import {CmykaColor} from '../type';
import {parseCmyka, rgbaToCmyka, roundCmyka} from '../color/model/cmyk';
import {parseCmykaText, rgbaToCmykaString} from '../color/model/cmyk-text';
import {Color} from '../color';

declare module '../color' {
    interface Color {
        /**
         * Converts a color to CMYK color space and returns an object.
         * https://drafts.csswg.org/css-color/#cmyk-colors
         * https://lea.verou.me/2009/03/cmyk-colors-in-css-useful-or-useless/
         */
        toCmyk(): CmykaColor;

        /**
         * Converts a color to CMYK color space and returns a string.
         * https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/device-cmyk()
         */
        toCmykString(): string;
    }
}

/**
 * A plugin adding support for CMYK color space.
 * https://lea.verou.me/2009/03/cmyk-colors-in-css-useful-or-useless/
 * https://en.wikipedia.org/wiki/CMYK_color_model
 */
const cmykPlugin: Plugin = (ColorClass: typeof Color, parsers): void => {
    ColorClass.prototype.toCmyk = function() {
        return roundCmyka(rgbaToCmyka(this.rgba));
    };

    ColorClass.prototype.toCmykString = function() {
        return rgbaToCmykaString(this.rgba);
    };

    parsers.object.push([parseCmyka, 'cmyk']);
    parsers.string.push([parseCmykaText, 'cmyk']);
};

export default cmykPlugin;
